<template>
  <div class="layout">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-container">
        <div class="logo">
          <router-link to="/">电商平台</router-link>
        </div>
        
        <div class="nav-menu">
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link to="/products" class="nav-item">商品列表</router-link>
        </div>
        
        <div class="header-right">
          <div class="search-box">
            <el-input placeholder="搜索商品" v-model="searchKeyword" class="search-input">
              <template #append>
                <el-button icon="Search" @click="handleSearch"></el-button>
              </template>
            </el-input>
          </div>
          
          <div class="user-actions">
            <template v-if="userStore.isLoggedIn">
              <el-dropdown>
                <span class="user-info">
                  <el-avatar :size="32">
                    {{ userStore.userInfo?.username?.charAt(0) || '用户' }}
                  </el-avatar>
                  <span>{{ userStore.userInfo?.username || '用户' }}</span>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="goToOrders">我的订单</el-dropdown-item>
                    <el-dropdown-item @click="goToProfile">个人中心</el-dropdown-item>
                    <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
            
            <template v-else>
              <router-link to="/login" class="action-link">登录</router-link>
              <router-link to="/register" class="action-link">注册</router-link>
            </template>
          </div>
          
          <router-link to="/cart" class="cart-link">
            <el-badge :value="cartStore.totalItems" :max="99">
              <el-icon size="24">
                <ShoppingCart />
              </el-icon>
            </el-badge>
          </router-link>
        </div>
      </div>
    </el-header>
    
    <!-- 主内容区 -->
    <el-main class="main">
      <router-view />
    </el-main>
    
    <!-- 页脚 -->
    <el-footer class="footer">
      <div class="footer-content">
        <div class="footer-links">
          <a href="#">关于我们</a>
          <a href="#">联系我们</a>
          <a href="#">隐私政策</a>
          <a href="#">用户协议</a>
        </div>
        <div class="copyright">
          © 2023 电商平台 版权所有
        </div>
      </div>
    </el-footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../store'
import { useCartStore } from '../store'
import { ShoppingCart } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const cartStore = useCartStore()

// 初始化购物车数据
onMounted(() => {
  cartStore.loadCart()
})

// 搜索关键词
const searchKeyword = ref('')

// 处理搜索
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    const encodedKeyword = encodeURIComponent(searchKeyword.value.trim())
    router.push({ path: '/products', query: { keyword: encodedKeyword } })
  }
}

// 跳转到订单列表
const goToOrders = () => {
  router.push('/orders')
}

// 跳转到个人中心
const goToProfile = () => {
  // 这里可以跳转到个人中心页面
  ElMessage.info('个人中心功能暂未实现')
}

// 处理退出登录
const handleLogout = async () => {
  try {
    // 调用退出登录接口
    // 即使退出登录失败，也清除本地的用户信息
    userStore.logout()
    // 退出登录时清空购物车
    cartStore.clearCart()
    ElMessage.success('退出登录成功')
    router.push('/')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}
</script>

<style scoped>
.layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
}

.logo {
  font-size: 24px;
  font-weight: bold;
}

.logo a {
  color: #1890ff;
  text-decoration: none;
}

.nav-menu {
  display: flex;
  gap: 20px;
}

.nav-item {
  font-size: 16px;
  color: #333;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-item:hover {
  color: #1890ff;
  background-color: #f5f5f5;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.search-box {
  position: relative;
}

.search-input {
  width: 200px;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.action-link {
  color: #333;
  text-decoration: none;
  font-size: 14px;
}

.action-link:hover {
  color: #1890ff;
}

.cart-link {
  color: #333;
  cursor: pointer;
  position: relative;
}

.cart-link:hover {
  color: #1890ff;
}

.main {
  flex: 1;
  padding: 20px 0;
}

.footer {
  background-color: #f5f5f5;
  padding: 20px 0;
  margin-top: auto;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  text-align: center;
}

.footer-links {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 10px;
}

.footer-links a {
  color: #666;
  text-decoration: none;
  font-size: 14px;
}

.footer-links a:hover {
  color: #1890ff;
}

.copyright {
  color: #999;
  font-size: 14px;
}
</style>